<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/11 0011
 * Time: 下午 2:55
 */

namespace app\sports_admin\service;

use app\exception\TrainException;
use app\sports_admin\model\Train as TrainModel;
class Train extends Base
{
    /**
     * 添加验证
     */
    public static function trainInsert()
    {
        $input = self::getParams();
        $bool = TrainModel::create($input);
        if(!$bool){
            throw new TrainException(['errorCode'=>400,'msg'=>'添加失败']);
        }
        return json(['msg'=>'添加成功','code'=>200]);
    }
    /**
     * 参数
     */
    private static function getParams()
    {
        $params = self::getHttpRequestPrograms()->param();
        $input['name'] = $params['name'];
        $input['customer_id'] = $params['customer_id'];
        $input['content'] = $params['content'];
        $input['address'] = $params['address'];
        $input['phone_name'] = $params['phone_name'];
        $input['phone'] = $params['phone'];
        $bool = TrainModel::where('phone',$params['phone'])->find();
        if($bool){
            throw new TrainException(['errorCode'=>400,'msg'=>'重复的手机号']);
        }
        $input['start'] = strtotime($params['start']);
        $input['end'] = strtotime($params['end']);
        $input['url'] = $params['url'];
        $input['email'] = $params['email'];
        $lnglat = explode(',',$params['lnglat']);
        $input['longitude'] = $lnglat[0];
        $input['latitude'] = $lnglat[1];
        $city = $params['province'];
        $input['password'] = authcode($params['password'],'ENCODE',$params['phone'],0);
        $input['province'] = intval(isset($city[0]))>0 ? intval($city[0]) : '';
        $input['city'] = intval(isset($city[1]))>0 ? intval($city[1]) : '';
        $input['district'] = intval(isset($city[2]))>0?intval($city[2]) : '';
        $input['img'] = $params['img1'].','.$params['img2'];
        return $input;
    }
}